package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据商品分类查询规格组
     * http://api.leyou.com/api/item/spec/groups/76
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = specService.querySpecGroups(cid);
        if (specGroups != null && specGroups.size() > 0) {
            return ResponseEntity.ok(specGroups);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    /**
     * 新增规格组
     * http://api.leyou.com/api/item/spec/group
     * @param specGroup
     * @return
     */
    @PostMapping("group")
    public ResponseEntity addSpecGroup(@RequestBody SpecGroup specGroup) {
        specService.addSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();// 201
    }

    /**
     * 更新规格组
     * http://api.leyou.com/api/item/spec/group
     * @param specGroup
     * @return
     */
    @PutMapping("group")
    public ResponseEntity updateSpecGroup(@RequestBody SpecGroup specGroup) {
        specService.updateSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();// 201
    }

    /**
     * 删除规格组
     * http://api.leyou.com/api/item/spec/group/1
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity updateSpecGroup(@PathVariable("id") Long id) {
        specService.deleteSpecGroup(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();// 204
    }

    /**
     * 根据规格组查询规格参数
     * http://api.leyou.com/api/item/spec/params?gid=?
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParams(@RequestParam(value = "gid", required = false) Long gid,
                                                           @RequestParam(value = "cid", required = false) Long cid,
                                                           @RequestParam(value = "searching", required = false) Boolean searching,
                                                           @RequestParam(value = "generic", required = false) Boolean generic) {
        List<SpecParam> specParams = specService.querySpecParams(gid, cid, searching, generic);
        if (specParams != null && specParams.size() > 0) {
            return ResponseEntity.ok(specParams);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();// 204
    }

    /**
     * 新增规格参数信息
     * http://api.leyou.com/api/item/spec/param
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParam specParam) {
        specService.addSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();// 201
    }

    /**
     * 更新规格参数信息
     * http://api.leyou.com/api/item/spec/param
     * @param specParam
     * @return
     */
    @PutMapping("param")
    public ResponseEntity updateSpecParam(@RequestBody SpecParam specParam) {
        specService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();// 201
    }

    /**
     * 删除规格参数信息
     * http://api.leyou.com/api/item/spec/param/33
     * @param id
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity deleteSpecParam(@PathVariable("id") Long id) {
        specService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();// 204
    }

}
